package com.yunduansing.core.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yunduansing.core.commons.DbCommon;
import com.yunduansing.core.commons.DbCommon.Grid;
import com.yunduansing.core.config.constant.SysConstant;
import com.yunduansing.core.dao.AccountDao;
import com.yunduansing.core.dao.redis.JedisClient;
import com.yunduansing.core.dto.AccountDto;
import com.yunduansing.core.model.AccountModel;
import com.yunduansing.core.model.SysUser;
import com.yunduansing.core.service.AccountService;
import com.yunduansing.core.utils.EncryptUtil;
import com.yunduansing.core.utils.ResultUtil;
@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountDao dao;
	private Logger log=LoggerFactory.getLogger(AccountServiceImpl.class);

	public ResultUtil login(String account, String password) {
		try{
			if(account.isEmpty())
			{
				log.error("error:登录异常——账号为空");
				return ResultUtil.init(false, "账号不能为空");
			}
			if(password.isEmpty())
			{
				log.error("error:登录异常——密码为空");
				return ResultUtil.init(false, "密码不能为空");
			}
			SysUser user=dao.login(account);
			if(user==null)
				return ResultUtil.init(false, "账号或密码错误");
			String salt=user.getSalt();
			if(EncryptUtil.validatePassword(password, user.getPassword(), salt)){
				user.setPassword("");
				log.info("success:登录成功，账号为："+account);
				return ResultUtil.init(true,"",user);
			}else{
				log.error("error:登录异常——登录失败");
				return ResultUtil.init(false,"账号或密码错误");
			}
		}catch(Exception e){
			System.out.println("登录"+e.getMessage());
			return ResultUtil.init(e); 
		}
	}

	public ResultUtil resetPassword(String newPwd,String oldPwd, int id) {
		if(dao.verifyByIdAndPwd(id, oldPwd)==0){
			log.error("error:修改密码异常——原密码不正确");
			return ResultUtil.init(false,"原密码不正确");
		}
		if(!dao.resetPassword(newPwd, id)){
			log.error("error:修改密码异常——修改失败");
			return ResultUtil.init(false,"修改失败");
		}else{
			log.info("info:修改密码成功");
			return ResultUtil.init(true,"修改成功");
		}
	}

	public AccountModel getByKey(Object key) {
		return dao.getByKey(key);
	}

	@Transactional
	public ResultUtil save(AccountModel entity) {
		try{
			if(entity.getId()>0)
				return ResultUtil.init(dao.update(entity)>0);
			else{
				//生成密码盐
				String salt=EncryptUtil.getSalt();
				entity.setPassword(EncryptUtil.createHash(entity.getPassword(), salt));
				return ResultUtil.init(dao.add(entity)>0);
			}
		}catch(Exception e){
			return ResultUtil.init(e);
		}
	}

	public ResultUtil deleteByKeys(List<Object> keys) {
		try{
			return ResultUtil.init(dao.deleteByKeys(keys)>0);
		}catch(Exception e){
			return ResultUtil.init(e);
		}
	}

	public Object pagination(Grid grid, Object search) {
		DbCommon.Pager pager=new DbCommon.Pager();
		pager.setIndex(grid.getIndex());
		pager.setData(dao.pagination(grid, search));
		pager.setRows(dao.pageCount(search));
		return pager;
	}

	@Transactional
	public ResultUtil saveWithGroupAndRole(AccountModel model,List<Integer> groupIds,List<Integer> roleIds) {
		try{
			if(model.getId()>0){
				dao.deleteAccountGroup(model.getId());
				dao.deleteAccountRole(model.getId());
				dao.update(model);
			}else{
				dao.add(model);
			}
			if(groupIds.size()>0)
				dao.saveAccountGroup(model.getId(), groupIds);
			if(roleIds.size()>0)
				dao.saveAccountRole(model.getId(), roleIds);
			return ResultUtil.init();
		}catch(Exception e){
			return ResultUtil.init(e);
		}
	}

	public AccountDto getAccountWithGroupAndRole(int id) {
		return dao.getAccountWithGroupAndRole(id);
	}

	public String getSalt(String account) {
		return dao.getSalt(account);
	}

}
